﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Maticsoft.DAL
{
	/// <summary>
	/// 数据访问类:keyinfo
	/// </summary>
	public partial class keyinfo
	{
		public keyinfo()
		{}
        /// <summary>
        /// 根据条件除数据
        /// </summary>
        public bool DeleteByWhere(string where)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from keyinfo ");

            if (!string.IsNullOrEmpty(where))
            {
                strSql.Append(" where " + where);
            }

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("keyInfoId", "keyinfo"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int keyInfoId)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from keyinfo");
			strSql.Append(" where keyInfoId=@keyInfoId");
			SqlParameter[] parameters = {
					new SqlParameter("@keyInfoId", SqlDbType.Int,4)
			};
			parameters[0].Value = keyInfoId;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}
        public bool Existsbystuid(int stuid,int type)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from keyinfo");
            strSql.Append(" where stuId=@stuId and type=@type");
            SqlParameter[] parameters = {
					new SqlParameter("@stuId", SqlDbType.Int,4),					
					new SqlParameter("@type", SqlDbType.Int,4)
			};
            parameters[0].Value =stuid;
            parameters[1].Value = type;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        public bool Exists(Maticsoft.Model.keyinfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from keyinfo");
            strSql.Append(" where stuId=@stuId and tid=@tid and type=@type");
            SqlParameter[] parameters = {
					new SqlParameter("@stuId", SqlDbType.Int,4),
					new SqlParameter("@tid", SqlDbType.Int,4),
                    new SqlParameter("@type", SqlDbType.Int,4)
			};
            parameters[0].Value = model.stuId;
            parameters[1].Value = model.tid;
            parameters[2].Value = model.type;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Maticsoft.Model.keyinfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into keyinfo(");
			strSql.Append("stuId,tid,answer,fjurl,type)");
			strSql.Append(" values (");
			strSql.Append("@stuId,@tid,@answer,@fjurl,@type)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@stuId", SqlDbType.Int,4),
					new SqlParameter("@tid", SqlDbType.Int,4),
					new SqlParameter("@answer", SqlDbType.VarChar,50),
					new SqlParameter("@fjurl", SqlDbType.VarChar,100),
					new SqlParameter("@type", SqlDbType.Int,4)};
			parameters[0].Value = model.stuId;
			parameters[1].Value = model.tid;
			parameters[2].Value = model.answer;
			parameters[3].Value = model.fjurl;
			parameters[4].Value = model.type;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Maticsoft.Model.keyinfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update keyinfo set ");			
			strSql.Append("answer=@answer,");
			strSql.Append("fjurl=@fjurl ");			
            strSql.Append(" where stuId=@stuId and tid=@tid and type=@type");
			SqlParameter[] parameters = {
					
					new SqlParameter("@answer", SqlDbType.VarChar,50),
					new SqlParameter("@fjurl", SqlDbType.VarChar,100),									
                    new SqlParameter("@stuId", SqlDbType.Int,4),
					new SqlParameter("@tid", SqlDbType.Int,4),
                    new SqlParameter("@type", SqlDbType.Int,4)
                                        };
            parameters[0].Value = model.answer;
            parameters[1].Value = model.fjurl;
            parameters[2].Value = model.stuId; 
			parameters[3].Value =model.tid;
			parameters[4].Value = model.type;		

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int keyInfoId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from keyinfo ");
			strSql.Append(" where keyInfoId=@keyInfoId");
			SqlParameter[] parameters = {
					new SqlParameter("@keyInfoId", SqlDbType.Int,4)
			};
			parameters[0].Value = keyInfoId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string keyInfoIdlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from keyinfo ");
			strSql.Append(" where keyInfoId in ("+keyInfoIdlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.keyinfo GetModel(int keyInfoId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 keyInfoId,stuId,tid,answer,fjurl,type from keyinfo ");
			strSql.Append(" where keyInfoId=@keyInfoId");
			SqlParameter[] parameters = {
					new SqlParameter("@keyInfoId", SqlDbType.Int,4)
			};
			parameters[0].Value = keyInfoId;

			Maticsoft.Model.keyinfo model=new Maticsoft.Model.keyinfo();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["keyInfoId"]!=null && ds.Tables[0].Rows[0]["keyInfoId"].ToString()!="")
				{
					model.keyInfoId=int.Parse(ds.Tables[0].Rows[0]["keyInfoId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["stuId"]!=null && ds.Tables[0].Rows[0]["stuId"].ToString()!="")
				{
					model.stuId=int.Parse(ds.Tables[0].Rows[0]["stuId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["tid"]!=null && ds.Tables[0].Rows[0]["tid"].ToString()!="")
				{
					model.tid=int.Parse(ds.Tables[0].Rows[0]["tid"].ToString());
				}
				if(ds.Tables[0].Rows[0]["answer"]!=null && ds.Tables[0].Rows[0]["answer"].ToString()!="")
				{
					model.answer=ds.Tables[0].Rows[0]["answer"].ToString();
				}
				if(ds.Tables[0].Rows[0]["fjurl"]!=null && ds.Tables[0].Rows[0]["fjurl"].ToString()!="")
				{
					model.fjurl=ds.Tables[0].Rows[0]["fjurl"].ToString();
				}
				if(ds.Tables[0].Rows[0]["type"]!=null && ds.Tables[0].Rows[0]["type"].ToString()!="")
				{
					model.type=int.Parse(ds.Tables[0].Rows[0]["type"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select keyInfoId,stuId,tid,answer,fjurl,type ");
			strSql.Append(" FROM keyinfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}
        public DataSet GetList1(int stuid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select singleId,stitle,sA,sB,sC,sD,skey,sscore");
            strSql.Append(" FROM vsingle where stuid="+stuid);            
            return DbHelperSQL.Query(strSql.ToString());
        }
        public DataSet GetList2(int stuid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select mostId,mtitle,mA,mB,mC,mD,mkey,mscore");
            strSql.Append(" FROM vmost where stuid=" + stuid );
            return DbHelperSQL.Query(strSql.ToString());
        }
        public DataSet GetList3(int stuid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select judgeId,jtitle,jkey,jscore ");
            strSql.Append(" FROM vjudge where stuid=" + stuid );
            return DbHelperSQL.Query(strSql.ToString());
        }

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" keyInfoId,stuId,tid,answer,fjurl,type ");
			strSql.Append(" FROM keyinfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM keyinfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.keyInfoId desc");
			}
			strSql.Append(")AS Row, T.*  from keyinfo T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "keyinfo";
			parameters[1].Value = "keyInfoId";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

